package com.sky.mapper;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {
    /**
     * 用户提交订单
     *
     * @param order
     * @return
     */
    Long insert(Orders order);

    /**
     * 根据订单号查询订单
     *
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     *
     * @param orders
     */
    void update(Orders orders);

    /**
     * 订单的条件分页查询
     *
     * @param ordersPageQueryDTO
     * @return
     */
    List<OrderVO> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据订单id获取订单
     *
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    /**
     * 各个状态订单的数量统计
     *
     * @return
     */
    OrderStatisticsVO countOrders();

    /**
     * 超找超时未付款的订单
     *
     * @return
     */
    @Select("select * from  orders where status = 1 and order_time < #{outTime}")
    List<Orders> selectTimeOutOrder(LocalDateTime outTime);


    /**
     * 无法真实支付的无奈之举。根据订单号（时间戳）找到这个订单
     *
     * @param orderNumber
     * @return
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders queryOrderByOrderNumber(String orderNumber);

    /**
     * 查出当天的营业额
     *
     * @param begin
     * @param completed
     * @return
     */
    @Select("SELECT SUM(amount) FROM orders WHERE status = #{completed} AND DATE(order_time) = #{begin}")
    Double getTurnoverByDate(LocalDate begin, Integer completed);

    /**
     * 按条件查询订单数
     * @param today
     * @param status
     * @return
     */
    Integer getOrderCountByDate(LocalDate today, Integer status);

    /**
     * 找出这个时间段的订单
     * @param begin
     * @param end
     * @param status
     * @return
     */
    List<Orders> queryOrderByTimePeriod(LocalDate begin, LocalDate end, Integer status);
}
